
import axios from 'axios'
const qs = require('qs')
/**
 * ======================================================================
 * 【基本说明】：
 * 网络请求模块,基于axios封装
 *
 * Created by Major_ on 18-9-12.
 * ======================================================================
 */

const https = {
    /** get 请求
   * @param  {接口地址} url
   * @param  {请求参数} params
   */
  async get (url, data) {
    try {
      let res = await axios.get(url, {params: data})
      res = res.data
      return new Promise((resolve) => {
        if (res.code) {
          resolve(res)
        } else {
          resolve(res)
        }
      })
    } catch (err) {
      alert('服务器出错')
      console.log(err)
    }
  },
  /** post 请求
   * @param  {接口地址} url
   * @param  {请求参数} params
   */
  async post (url, data) {
    try {
      let res = await axios.post(url, qs.stringify(data))
      res = res.data
      return new Promise((resolve, reject) => {
        if (res.code) {
          resolve(res)
        } else {
          reject(res)
        }
      })
    } catch (err) {
      alert('服务器出错')
      console.log(err)
    }
  },
}

export default https